<template>
	<el-container>
		<el-header>
			<div class="left-panel">
				<el-button type="primary" v-if="type==1" @click="type=2">添加活动</el-button>
				<el-button v-else @click="type=1">返回列表</el-button>
			</div>
		</el-header>
		<el-main v-if="type==1" class="nopadding">
			<scTable ref="table" :apiObj="apiObj" :params="params" stripe>
				<el-table-column label="ID" prop="id" width="60"></el-table-column>
				<el-table-column label="活动海报" prop="poster" width="100">
					<template #default="scope">
						<el-image style="width: 80px; height: 110px" :src="scope.row.poster" fit="cover">
						</el-image>
					</template>
				</el-table-column>
				<el-table-column label="活动名称" prop="name"></el-table-column>
				<el-table-column label="平台类型" prop="type2">
					<template #default="scope">{{scope.row.type2==1?'抖音活动':'快手活动'}}</template>
				</el-table-column>
				<el-table-column label="活动类型" prop="type">
					<template #default="scope">
						<span v-if="scope.row.type==1">折扣卷</span>
						<span v-else-if="scope.row.type==2">满减卷</span>
						<span v-else-if="scope.row.type==3">礼品卷</span>
						<span v-else>红包卷</span>
					</template>
				</el-table-column>

				<!-- <el-table-column label="状态" prop="status" width="80">
					<template #default="scope">
						<span v-if="scope.row.status==0">未审核</span>
						<span style="color: #67C23A" v-else-if="scope.row.status==1">审核通过</span>
						<span style="color: #F56C6C" v-else>审核不通过</span>
					</template>

				</el-table-column> -->


				<el-table-column label="操作"  align="right" width="300">
					<template #default="scope">
						<el-button type="text" size="small" @click="table_show(scope.row)">活动数据</el-button>
						<el-button type="text" size="small" @click="downloadIamge(scope.row.poster,scope.row.name)">下载推广海报</el-button>


							<el-button type="text" size="small" @click="table_edit(scope.row)">编辑</el-button>
							<el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row)">
								<template #reference>
									<el-button type="text" size="small">删除</el-button>
								</template>
							</el-popconfirm>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
		<el-main v-else-if="type==2">
			<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="80px" label-position="top">
				<el-row :gutter="20">
					<el-col :span="12">
						<el-form-item label="活动名称" prop="name">
							<el-input v-model="form.name"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="活动类型" prop="type">
							<el-select v-model="form.type" placeholder="请选择" @change="refreshStatus">
							    <el-option v-for="item in typeData"
										   :key="item.value"
										   :label="item.label"
										   :value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="平台类型" prop="type2">
							<el-select v-model="form.type2" :disabled="form.id>0" placeholder="请选择" @change="refreshStatus">
								<el-option v-for="item in type2Data"
										   :key="item.value"
										   :label="item.label"
										   :value="item.value"></el-option>
							</el-select>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="跳转地址" prop="goto">
							<el-radio-group v-model="form.goto">
								<el-radio v-model="form.goto" label='1'>商家主页</el-radio>
								<el-radio v-model="form.goto" label='2'>卡券列表</el-radio>
							</el-radio-group>
						</el-form-item>
					</el-col>

					<el-col :span="12">
						<el-form-item label="发放数量" prop="grant_num">
							<el-input  v-model.number="form.grant_num"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="可领取数量" prop="dayget_num">
							<el-input  v-model.number="form.dayget_num"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="可领取总数量" prop="everybodyget_num">
							<el-input  v-model.number="form.everybodyget_num"></el-input>
						</el-form-item>
					</el-col>
					<el-col :span="12" v-if="form.type2==1">
						<el-form-item label="是否@商家" prop="isatstore">
							<el-switch v-model="form.isatstore" active-value="1" inactive-value="0"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :span="12" v-if="form.type2==1">
						<el-form-item label="是否挂载poi地址" prop="ispoi">
							<el-switch v-model="form.ispoi" active-value="1" inactive-value="0"></el-switch>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="结束时间" prop="ent_time">
							<el-date-picker
							      v-model="form.ent_time"
							      type="date"
								  value-format="YYYY-MM-DD"
							      placeholder="选择日期">
							    </el-date-picker>
						</el-form-item>
					</el-col>
					<el-col :span="12">
						<el-form-item label="活动介绍" prop="introduce">
							<el-input type="textarea" v-model="form.introduce"></el-input>
						</el-form-item>
					</el-col>

					<el-col :span="12" v-if="!form.id">
						<el-form-item label="活动海报" prop="poster">
							<sc-upload v-model="form.poster" :apiObj="uploadApi" :on-success="success" :on-progress="progress" :width="220">

								<div class="custom-empty">
									<i class="el-icon-upload"></i>
									<p>上传海报</p>
								</div>
							</sc-upload>
						</el-form-item>
					</el-col>
					<el-col :span="24">
						<el-button type="primary" :loading="loading" @click="tijiao">提交</el-button>
					</el-col>
				</el-row>
			</el-form>
		</el-main>
		<el-main v-else-if="type==3">
			<Chartlist ref="Chartlist"></Chartlist>
		</el-main>
	</el-container>
</template>

<script>
import Chartlist from "../template/chartlist";

export default {
	name: "hdlist",
	data() {
		return {
			type:1,
			apiObj:'',
			store_id:'',
			params:{},
			uploadApi:this.$API.upload.img,
			typeData:[{
				value: 1,
				label: '折扣券'
			},{
				value: 2,
				label: '满减券'
			},{
				value: 3,
				label: '礼品券'
			},{
				value: 4,
				label: '红包券'
			}],
			type2Data:[{
				value: 1,
				label: '抖音活动'
			},{
				value: 2,
				label: '快手活动'
			}],
			form:{
				id:'',
				store_id:'',
				user_id:'',
				name:'',
				type:'',
				type2:'',
				goto:'1',
				grant_num:'',
				dayget_num:'',
				everybodyget_num:'',
				isatstore:"0",
				ispoi:"0",
				ent_time:'',
				introduce:'',
				poster:''
			},
			loading:false,
			rules:{
				name:[
					{required: true, message: '请输入活动名称'}
				],
				type: [
					{required: true, message: '请选择活动类型'}
				],
				type2: [
					{required: true, message: '请选择平台类型'}
				],
				grant_num: [
					{required: true, type: 'number', message: '请输入发放数量'}
				],
				dayget_num: [
					{required: true, type: 'number', message: '请输入可领取数量'}
				],
				everybodyget_num: [
					{required: true, type: 'number', message: '请输入可领取总数量'}
				],
				ent_time: [
					{required: true, message: '请选择结束时间'}
				],
				introduce: [
					{required: true, message: '请输入活动介绍'}
				],
				poster: [
					{required: true, message: '请上传海报'}
				],
			}
		}
	},
	components: {
		Chartlist,

	},
	methods: {
		setData(data){

			this.apiObj = this.$API.store.hdlist
			this.form.store_id = data.id
			this.form.user_id = data.user_id
			this.params = {store_id:data.id,user_id:data.user_id}
			// console.log('hd',data)
		},
		refreshStatus(){
			this.$forceUpdate()
		},
		table_show(data){
			this.type=3
			this.$nextTick(() => {
				//这里应该再次根据ID查询详情接口
				this.$refs.Chartlist.getData(data)
			})
		},
		table_xiazai(data){
			console.log('下载图片')
			var a = document.createElement('a')
			a.download = name || 'pic'
			// 设置图片地址
			a.href = data.poster;
			a.click();
		},
		downloadIamge(imgsrc, name) {//下载图片地址和图片名
			this.$API.xiazai.image.get({url:imgsrc}).then((res)=>{
				const fileName = '海报下载_'+name+'_'+new Date().getTime()+'.jpg';
				const myBlob = new Blob([res], { type: "image/jpeg" });
				const link = document.createElement("a");
				link.href = URL.createObjectURL(myBlob);
				link.download = fileName;
				link.click();
				link.remove();
				URL.revokeObjectURL(link.href);
			})

		},
		table_del(data){

			this.$API.store.hddel.post({id: data.id}).then(res=>{
				if(res.code==200){
					this.$message.success(res.msg)
					this.$refs.table.getData()
				} else {
					this.$alert(res.msg, "提示", {type: 'error'})
				}
			})
		},
		table_edit(data){
			console.log(data)
			this.type=2
			this.form.goto = data.goto==1?'1':'2'
			this.form.id = data.id
			this.form.store_id = data.store_id
			this.form.user_id = data.user_id
			this.form.name = data.name
			this.form.type = data.type
			this.form.type2 = data.type2
			this.form.grant_num=data.grant_num
			this.form.dayget_num=data.dayget_num
			this.form.everybodyget_num=data.everybodyget_num
			this.form.isatstore=data.isatstore==1?"1":"0"
			this.form.ispoi=data.ispoi==1?"1":"0"
			this.form.ent_time=data.ent_time
			this.form.introduce=data.introduce
			this.form.poster=data.poster
		},
		shenghe(row , type){
			var data = {id:row.id,status:type}
			this.$API.store.vshenghe.post(data).then(res=>{
				if(res.code==200){
					this.$message.success(res.msg)
					this.$refs.table.getData()
				} else {
					this.$alert(res.msg, "提示", {type: 'error'})
				}
			});
		},
		progress(event, file){
			console.log('jjjj',file.percentage.toFixed(0))
		},
		submitUpload() {
			this.$refs.upload.submit();
		},
		success(response){
			this.form.poster = response.data
			// console.log(response);
			return false;
		},
		tijiao(){
			console.log(this.form);
			let that=this

			this.$refs.dialogForm.validate((valid) => {
				if (valid) {
					this.loading = true
					// this.form.ispoi = this.form.ispoi?1:0
					// this.form.isatstore = this.form.isatstore?1:0
					this.$API.store.hdedit.post(this.form).then(res=>{
						if(res.code==200){
							that.type=1
							that.loading = false
							this.$message.success(res.msg)
							// this.$refs.table.getData()
						} else {
							this.$alert(res.msg, "提示", {type: 'error'})
						}
					});
				}else{
					return false;
				}
			})
		}
	}
}
</script>

<style scoped>
.el-card+.el-card {margin-top: 15px;}

.imglist {margin-bottom:0;}
.imglist .el-col+.el-col {margin-left: 10px;}
.custom-empty {width: 100%;height: 100%;line-height: 1;display: flex;flex-direction: column;align-items: center;justify-content: center;}
.custom-empty i {font-size: 40px;color: #8c939d;}
.custom-empty p {font-size: 12px;font-weight: normal;color: #8c939d;margin-top: 10px;}
</style>
